﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Net;
using System.Text;
using System.IO;
using FTchina.Logic;
using FTchina.Library;

public partial class ui_qudao_PswReset : BasePage
{
    public static CookieContainer cc = new CookieContainer();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 批量重置工号的密码;
    /// </summary>
    /// <param name="al"></param>
    private void ReSetPassWord(ArrayList al)
    {


        //  string ResetUser = "v-wanghy121-5";
        string ReSetPassWord = "123456";//将密码重置为





        string uname = "zhudq1";//具有渠道管理权限的用户名密码
        string upsw = "JINch0806";



        string loginurl = "http://www.sd.unicom.local/pkmslogin.form";
        string poststr = "userPortal=&username=" + uname + "&password=" + upsw + "&login-form-type=pwd";
        //登录
        PostHttphtml(loginurl, Encoding.UTF8, poststr);
        // LogNet.LogBLL.debug("登录" + PostHttphtml(loginurl, Encoding.UTF8, poststr));


        //全国应用
        string quanguourl = "http://www.sd.unicom.local/was/wps/myportal/dy/!ut/p/c1/04_SB8K8xLLM9MSSzPy8xBz9CP0os3iPoDBvczdTEwN_8yBHA09vbwu3kBBfA39HI_1wkA4kFQYWFi5AFSauIQGBfgbOfkYQeQMcwNFA388jPzdVvyA7O83RUVERAJhyAVY!/dl2/d1/L2dJQSEvUUt3QS9ZQnB3LzZfSFJWSzdGNTQwR1U5MjBJNlFET0kwTTMwODU!/";
        GetHttphtml(quanguourl, Encoding.UTF8);
        // LogNet.LogBLL.debug("全国应用" + GetHttphtml(quanguourl, Encoding.UTF8));

        string url1 = "http://www.sd.unicom.local/pkmscdsso?http://www.unicom.local/wpsv6/wps/myportal/newunicom/mpapps";

        string url2 = "http://www.unicom.local/wpsv6/wps/myportal/newunicom/mpapps?PD-REFERER=www.sd.unicom.local&PD-ID=OAzQlwVoPGIniYRLSfjH+SkdoWsmurIb4BElYYXiTP+qT5KXmyGX2WOYKDfLnzupBxki9mReTXyOnfGuIZB1zQ1/fdIUVYf2Ew6aRbx8jTSNyiSjXGlyzn8MfCWv8gfxlTbM7inNGtU= ";
        string url3 = "   http://www.sd.unicom.local/st/PageTransfer/loginst.html ";
        //LogNet.LogBLL.debug("跳转1" + GetHttphtml(url1, Encoding.UTF8));
        //LogNet.LogBLL.debug("跳转2" + GetHttphtml(url2, Encoding.UTF8));
        //LogNet.LogBLL.debug("跳转3" + GetHttphtml(url3, Encoding.UTF8));
        GetHttphtml(url1, Encoding.UTF8);
        GetHttphtml(url2, Encoding.UTF8);
        GetHttphtml(url3, Encoding.UTF8);


        //渠道管理
        string qdglurl = "http://www.unicom.local/wpsv6/wps/myportal/newunicom/!ut/p/c5/04_SB8K8xLLM9MSSzPy8xBz9CP0os3gLI98Qb8tgYxNzX3dnA09T8xBTXx8DIwMDQ6B8JLK8ZYibgaevi3OAl5G7gX-QOQHd4SD7cOp39jbEKw82HyRvgAM4Guj7eeTnpuoX5EYYZAakKwIARH5DdQ!!/dl3/d3/L2dJQSEvUUt3QS9ZQnZ3LzZfODJNVEs5UzM0Tks0ODBJNThFNEEyTjMwTzQ!/";
        GetHttphtml(qdglurl, Encoding.UTF8);
        // LogNet.LogBLL.debug("渠道管理" + GetHttphtml(qdglurl, Encoding.UTF8));


        //登录渠道管理
        string qdgllogin = "http://www.unicom.local/qdyhglsso/ldappwd/auth/mhLogin.do";
        GetHttphtml(qdgllogin, Encoding.UTF8);
        // LogNet.LogBLL.debug(GetHttphtml(qdgllogin, Encoding.UTF8));



        //测试系统是否存在此用户
        string Chkurl = " http://www.unicom.local/ldappwd/user/listUser.do?isqduser=true";
        string Chkstr = "start=0&limit=15&uid={0}&sort=cn&dir=DESC";
        string Chkresult = string.Empty;//检测结果



        //改密码
        string gmmurl = "http://www.unicom.local/ldappwd/user/resetUserPwd.do";
        string postrmm = "userDN=uid%3D{0}%2Ccn%3Dusers%2Cdc%3Dqd%2Cdc%3DUNICOM&cumail={0}&newpwd=" + ReSetPassWord + "&newpwdconfirm=" + ReSetPassWord;
        string rrtemp = string.Empty;
        foreach (string ResetUser in al)
        {
            //测试系统是否存在此用户            
            Chkresult = PostHttphtml(Chkurl, Encoding.UTF8, string.Format(Chkstr, ResetUser));
            if (Chkresult.Contains("[]"))
            {
                // LogNet.LogBLL.debug("CHK" + ResetUser + Chkresult);
                rrtemp = ResetUser + "无此工号  " + DateTime.Now.ToString() + "<br>";
                return;
            }
            PostHttphtml(gmmurl, Encoding.UTF8, string.Format(postrmm, ResetUser));
            rrtemp = ResetUser + "   " + ReSetPassWord + "   " + DateTime.Now.ToString() + "<br>";

           
           

            #region 记录日志
            LogHelper.Info(rrtemp);//记录到系统日志
            FTchina.Models.Qudao_Log mQudao_Log = new FTchina.Models.Qudao_Log();
            mQudao_Log.Id = 0;
            mQudao_Log.UAccount = ResetUser;
            mQudao_Log.Upsw = ReSetPassWord;
            mQudao_Log.Dt = DateTime.Now;
            mQudao_Log.Ip = RequestHelper.GetUserIp();
            mQudao_Log.UName = MasterHelper.UName;
            FTchina.BLL.Qudao_LogManager.AddOrUpdateQudao_Log(mQudao_Log);
            #endregion
            AlertLogic.ShowInfo(rrtemp);
        }

    }


    public static string PostHttphtml(string url, Encoding en, string poststr)
    {
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.CookieContainer = cc;
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "post";
        request.Timeout = 0xea60;
        request.KeepAlive = true;
        byte[] bytes = en.GetBytes(poststr);
        request.ContentLength = bytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), en);
        string str = reader.ReadToEnd();
        reader.Close();
        return str;



    }
    public static string GetHttphtml(string url, Encoding en)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
             request.CookieContainer = cc;
            request.Method = "GET";
            request.Accept = "*/*";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.KeepAlive = true;
            request.Timeout = 0xea60;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), en);
            cc.Add(response.Cookies);
            string str = reader.ReadToEnd();
            reader.Close();
            return str;
        }
        catch (Exception exception)
        {
            LogHelper.Error(exception.Message);
            return "";
        }
    }

    protected void btnok_Click(object sender, EventArgs e)
    {

        ArrayList al = new ArrayList();
        string words = txbusrs.Text.Trim();

        if (words.Contains(";"))
        {
            foreach (string str in words.Split(';'))
            {
                if (str.Trim().Length > 0) al.Add(str.Trim());
            }
        }
        else
        {
            al.Add(words);

        }
        ReSetPassWord(al);

    }
}